import requests
import re

from Constances import UserAgent

# 电影天堂
domain = 'https://www.dytt89.com'
header = {
    'user-agent': UserAgent
}
# verify=False: 关闭安全验证
resp = requests.get(url=domain, verify=False)
resp.encoding = 'gb2312'
# print(resp.text)
pageContent = resp.text

rule1 = re.compile(r'2023必看热片.*?'
                  r'<ul>(?P<movies>.*?)</ul>', re.S)
it = rule1.finditer(pageContent)
for item in it:
    # print(item.group("movies"))
    movieContent = item.group("movies")

rule2 = re.compile(r"<li><a href='(?P<url>.*?)'", re.S)
it = rule2.finditer(movieContent)
urlList = []
for item in it:
    # print(item.group("url"))
    urlList.append(item.group("url"))

# 请求子页面
# rule3 = re.compile(r'<div id="downlist" style="display:none">(?P<downlist>.*?)</div>', re.S)
rule3 = re.compile(r'<div id="downlist"  style="display:none">(?P<downlist>.*?)</div>', re.S)
rule4 = re.compile(r'href="(?P<magnet>.*?)">magnet', re.S)

for url in urlList:
    print('-------------------------------------------------------')
    realUrl = domain + url
    resp = requests.get(realUrl)
    resp.encoding = 'gb2312'
    detailContent = resp.text
    # print(detailContent)

    it = rule3.finditer(detailContent)
    for item in it:
        # print(item.group("downlist"))
        downlistContent = item.group("downlist")

    it = rule4.finditer(downlistContent)
    for item in it:
        print(item.group("magnet"))

    break
